#pragma once

enum colour {RED,BLACK};

template<class T>
struct RBTreeNode
{
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	T _data;

	colour _col;

	RBTreeNode(const T& data)
		:_left(nullptr),_right(nullptr),_parent(nullptr),_data(data),_col(RED)
	{}
};

template<class T,class Ref,class Ptr>
struct __RBTree_Iterator
{
	typedef RBTreeNode<T> Node;
	typedef __RBTree_Iterator<T, Ref, Ptr> Self;

	Node* _node;

	__RBTree_Iterator(Node* node)
		:_node(node)
	{}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			Node* leftMin = _node->_right;
			while (leftMin->_left)
				leftMin = leftMin->_left;

			_node = leftMin;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}
		return *this;
	}
};


template<class K, class T,class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef __RBTree_Iterator<T, T&, T*> Iterator;
	typedef __RBTree_Iterator<T,const T&,const T*> Const_Iterator;
	
	Iterator Begin()
	{
		Node* leftMin = _root;
		while (leftMin && leftMin->_left)
			leftMin = leftMin->_left;

		return Iterator(leftMin);
	}

	Iterator End()
	{
		return Iterator(nullptr);
	}

	Const_Iterator Begin() const
	{
		Node* leftMin = _root;
		while (leftMin && leftMin->_left)
			leftMin = leftMin->_left;

		return Iterator(leftMin);
	}

	Const_Iterator End() const
	{
		return Iterator(nullptr);
	}

	RBTree() = default;

	RBTree(const RBTree<K, T, KeyOfT>& t)
	{
		_root = copy(t._root);
	}

	RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t)
	{
		std::swap(_root, t._root);
		return *this;
	}

	~RBTree()
	{
		destroy(_root);
		_root = nullptr;
	}

	pair<Iterator,bool> Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return make_pair(Iterator(_root), false);
		}

		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(data) < kot(cur->_data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kot(data) > kot(cur->_data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return make_pair(Iterator(cur), true);
			}
		}
		
		cur = new Node(data);
		Node* newnode = cur;
		if (kot(data) < kot(parent->_data))
			parent->_left = cur;
		else
			parent->_right = cur;
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
		}

		_root->_col = BLACK;
		return make_pair(Iterator(newnode), false);
	}

	Iterator Find(const K& k)
	{
		KeyOfT kot;
		Node* cur = _root;
		while (cur)
		{
			if (k < kot(cur->_data))
			{
				cur = cur->_left;
			}
			else if(k > kot(cur->_data))
			{
				cur = cur->_right;
			}
			else
			{
				return Iterator(cur);
			}
		}
		return Iterator(nullptr);
	}
	
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		subL->_right = parent;

		Node* ppNode = parent->_parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}

			subL->_parent = ppNode;
		}
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		subR->_left = parent;
		Node* ppNode = parent->_parent;

		parent->_parent = subR;

		if (parent == _root)
		{
			_root = subR;
			_root->_parent = nullptr;
		}
		else
		{
			if (ppNode->_right == parent)
			{
				ppNode->_right = subR;
			}
			else
			{
				ppNode->_left = subR;
			}
			subR->_parent = ppNode;
		}
	}

	bool IsBalance()
	{
		if (_root == nullptr) return true;
		
		if (_root->_col != BLACK)
			return false;
		
		int blacksum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK) blacksum++;
			cur = cur->_left;
		}

		return check(_root, 0, blacksum);
	}

private:
	bool check(Node* root, int path, int blacksum)
	{
		if (root == nullptr)
		{
			if (path != blacksum)
				return false;

			return true;
		}
		
		if (root->_col == RED && root->_parent->_col == RED)
			return false;

		if (root->_col == BLACK)
			path++;

		return check(root->_left, path, blacksum)
			&& check(root->_right, path, blacksum);
	}

	void destroy(Node* root)
	{
		if (root == nullptr)
			return;
		
		destroy(root->_left);
		destroy(root->_right);
		delete root;
		root = nullptr;
	}
	
	Node* copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;

		Node* newnode = new Node(root->_data);
		newnode->_col = root->_col;

		newnode->_left = copy(root->_left);
		if (newnode->_left)
			newnode->_left->_parent = newnode;

		newnode->_right = copy(root->right);
		if (newnode->_right)
			newnode->_right->_parent = newnode;
		
		return newnode;
	}
private:
	Node* _root = nullptr;
};
